package cn.edu.zjgsu.ito.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.zjgsu.ito.exception.PwdWrongException;
import cn.edu.zjgsu.ito.exception.UserInexistException;
import cn.edu.zjgsu.ito.service.UserLoginServiceI;
import cn.edu.zjgsu.ito.util.Constant;
import cn.edu.zjgsu.ito.vo.UserLoginOkVo;
import cn.edu.zjgsu.ito.vo.UserLoginVo;

/**
 * 用户登录控制类
 * */
@Controller
@RequestMapping("/userLoginController")
public class UserLoginController {
	
	@Autowired
	private UserLoginServiceI userLoginService;
	
	
	/**
	 * 用户账号密码登录接口
	 * 根据用户的账号密码，进行登录操作
	 * @param userLoginVo 用户登录信息
	 * @return
	 * */
	@RequestMapping(value="/login",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> login(UserLoginVo userLoginVo,HttpSession session){
		Map<String,Object> result = new HashMap<String, Object>();
		
		try{
			UserLoginOkVo userLoginOkVo = userLoginService.userLogin(userLoginVo);
			session.setAttribute("userId", userLoginOkVo.getUserId());
			System.out.println("用户的session：" + session.getAttribute("userId"));
			result.put("msg", userLoginOkVo);
			result.put("code", Constant.OK);
		}catch (UserInexistException e) {
			result.put("msg", Constant.USERNAME_NOT_EXIST_MSG);
			result.put("code", Constant.USERNAME_NOT_EXIST_CODE);
		}catch(PwdWrongException e){
			result.put("msg",Constant.WRONG_PWD_MSG);
			result.put("code", Constant.WRONG_PWD_CODE);
		}
		
		return result;
	}
	
	/**
	 * 用户账号手机验证码登录接口
	 * 用户获取手机验证码
	 * @param userLoginVo 用户登录信息
	 * @return
	 * */
	@RequestMapping(value="/loginSendMessage",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> loginSendMessage(String username,HttpSession session){
		Map<String,Object> result = new HashMap<String, Object>();
		String code = userLoginService.loginSendMessage(username);
		System.out.println("code" + code);
		if(code.equals("error")){
			result.put("code",Constant.FAIL);
		}else{
			session.setAttribute("username",username);
			session.setAttribute("security", code);
			result.put("code",Constant.OK);
		}
		return result;
	}
	
	/**
	 * 用户手机验证码登录接口
	 * 输入手机验证码登录
	 * @param userLoginVo 用户登录信息
	 * @return
	 * */
	@RequestMapping(value="/loginMessage",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> loginMessage(String security,HttpSession session){
		Map<String,Object> result = new HashMap<String, Object>();
		String username = session.getAttribute("username").toString();
		String securitySession = session.getAttribute("security").toString();
		int msg = userLoginService.loginMessage(username,securitySession,security);
		if(msg == -1){
			result.put("code",Constant.FAIL);
		}else{
			session.setAttribute("userId", msg);
			result.put("msg", msg);
			result.put("code",Constant.OK);
		}
		return result;
	}
		
	
	/**
	 * 用户注册提交手机号接口
	 * @param userLoginVo 
	 * @return
	 * */
	@RequestMapping(value="/registerPhone",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> registerPhone(String phone,HttpSession session){
		Map<String,Object> result = new HashMap<String, Object>();
		
		String temp = userLoginService.registerPhone(phone);
		if(temp.equals("error")){
			result.put("code",Constant.FAIL);
		}else{
			session.setAttribute("username", phone);
			session.setAttribute("security", temp);
			result.put("code",Constant.OK);
		}
		return result;
	}
	
	/**
	 * 用户注册提交手机验证码接口
	 * @param  
	 * @return
	 * */
	@RequestMapping(value="/registerSecurity",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> registerSecurity(String security,HttpSession session){
		Map<String,Object> result = new HashMap<String, Object>();
		String securitySession = session.getAttribute("security").toString();
		if(securitySession.equals(security)){
			result.put("code",Constant.OK);
		}else{
			result.put("code",Constant.FAIL);
		}
		return result;
	}
	
	/**
	 * 用户注册提交密码成功注册接口
	 * @param  
	 * @return
	 * */
	@RequestMapping(value="/registerSuccess",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> registerSuccess(String password,HttpSession session){
		Map<String,Object> result = new HashMap<String, Object>();
	
		String username = session.getAttribute("username").toString();
		int userId = userLoginService.registerSuccess(username,password);
		
		result.put("msg", userId);
		result.put("code",Constant.OK);
		return result;
		
	}
	
	/**
	 * 用户退出
	 * @param  
	 * @return
	 * */
	@RequestMapping(value="/userExit",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> userExit(HttpSession session){
		Map<String,Object> result = new HashMap<String, Object>();
		session.removeAttribute("userId");
		result.put("code",Constant.OK);
		return result;
	}
	
}
